package kgc.crm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import kgc.crm.annotation.UserPower;
import kgc.crm.annotation.VerificationToken;
import kgc.crm.enums.PowerEnum;
import kgc.crm.mapper.CustomerMapper;
import kgc.crm.model.Customer;
import kgc.crm.service.ICustomerService;
import kgc.crm.util.JWTManager;
import kgc.crm.util.RequestUtil;
import kgc.crm.util.ResultUtil;
import kgc.crm.vo.LeadsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.swing.text.Segment;


@RestController
@RequestMapping("cust")
@CrossOrigin
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    //新增
    @UserPower(power = PowerEnum.SAVE)
    @VerificationToken
    @PostMapping("add")
    public ResponseEntity add(@RequestBody Customer customer, HttpServletRequest request) {
        String apiToken = RequestUtil.getApiToken(request);
        return customerService.add(customer,apiToken);
    }

    //修改
    @UserPower(power = PowerEnum.UPDATE)
    @VerificationToken
    @PostMapping("update")
    public ResponseEntity update(@RequestBody Customer customer) {
        return customerService.update(customer);
    }

    //删除
    @UserPower(power = PowerEnum.DELETE)
    @VerificationToken
    @PostMapping("delete")
    public ResponseEntity delete(@PathVariable String id){
        JSONObject object = JSON.parseObject(id);
        return customerService.delete(object.getInteger("customerId"));
       // return customerService.delete(id);
    }
    //查看
    @UserPower(power = PowerEnum.INDEX)
    @VerificationToken
    @PostMapping("select")
    public ResponseEntity select(@RequestBody String id,HttpServletRequest request){
        JSONObject object = JSON.parseObject(id);
            return customerService.select(object.getInteger("customerId"),RequestUtil.getApiToken(request));
    }
    //查看全部用户
    @UserPower(power = PowerEnum.INDEX)
    @VerificationToken
    @PostMapping("selectAll")
    public ResponseEntity selectAll(){
        return customerService.selectAll();
    }

    //高级筛选
    @UserPower(power = PowerEnum.INDEX)
    @VerificationToken
    @PostMapping("selectList")
    public ResponseEntity selectList(@RequestBody LeadsVo vo,HttpServletRequest request){
        return customerService.selectList(vo,RequestUtil.getApiToken(request));
    }
}
